/*
 *  JapaneseDetector.java
 *
 *
 *  Perl Version
 *    Jerome Mathieu
 *    Peter Turney
 *
 *  Java Version
 *    Chau, Sau Ming
 *
 *  May 25, 2000
 *
 *
 *  This file defines the JapaneseDetector class.  JapaneseDetector consists of
 *  one static function detect_jpn(String text) only.  This is used in the
 *  case of Unicode detected.
 *
 *  Heuristic:
 *
 *     - If the text contains at least one hiragana character, it is
 *       considered to be a text written in Japanese.
 *
 *  Usage:
 *    String text     = some_input_stream();  // raw text
 *    int return_code = JapansesDetector.detect_jpn(text);
 *
 *    return_code =  0  --> other than Japanese text
 *    return_code =  3  --> Japanese text
 */

import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;

public class JapaneseDetector
{
  public final static int UNKNOWN  = 0; //
  public final static int JAPANESE = 3; // SJIS or LAT

  
  /*
   *  detect_jpn
   *
   *  Return Language ID detected in a document in String format.
   *  Notice that it is the responsibility of the users the
   *  encoding the text.
   *
   *  Arguments
   *    1. (String) content of the text
   *    2. (String) input String encoding method (e.g. UTF-16).  Plese refer
   *                  to Java api for more information about String encoding
   *
   *  Return
   *    - (int) Language ID
   *
   *  detect_jpn(String text, String enc)
   */
  public static int detect_jpn(String text, String enc) throws UnsupportedEncodingException
  {
    try
    {
      return detect_jpn(text.getBytes(enc));
    }
    catch (UnsupportedEncodingException e)
    {
      throw e;
    }
  }
  
  
  /*
   *  byte_array_to_short_array
   *
   *  Return short array (short[]) converted from byte array (byte[]) passed
   *  as an argument.
   *
   *  Arguments
   *    1. (byte[]) content of the text
   *    2. (byte[]) content of the text without HTML tag
   *
   *  Return
   *    - (int) Character Code
   *
   *  byte_array_to_short_array(byte[] buf)
   */
  public static short[] byte_array_to_short_array(byte[] buf)
  {
    int size = buf.length;
    short[] arr_return = new short[size];
    short sht_temp;
    for (int i = 0; i < size; i++)
    {
      if (buf[i] < 0)
      {
        arr_return[i] = (short)(buf[i] + 256);
      }
      else
      {
        arr_return[i] = (short)buf[i];
      }
    }
    return arr_return;
  }
  
  
  /*
   *  detect_jpn
   *
   *  Return Language ID detected in a document in byte array.
   *
   *  Arguments
   *    1. (byte[]) content of the text
   *
   *  Return
   *    - (int) Language ID
   *
   *  detect_jpn(String text, String enc)
   */
  public static int detect_jpn(byte[] buf_text)
  {
    int whatlang;  // the language
    int pos_EOF;   // length of the text strings (length of the list of integer values
    short[] buf = byte_array_to_short_array(buf_text); // the text string converted into a list of integer values.
    int i = 0;     // the position in the list of integer values
    short c = 0;   // the value of the current integer value

    try
    {
      pos_EOF = buf_text.length;
      if (pos_EOF <= 0)
      { // Return -1 if no text
        return -1;
      }
    }
    catch (IllegalArgumentException e)
    {
      throw e;
    }
    whatlang = UNKNOWN;
    

    while((whatlang == UNKNOWN) && (i != pos_EOF))
    { // check for one Hiragana character
      c = buf[i];
      i++;
      if ((c >= 0x41) && (c <= 0x93))
      {
        c = buf[i];
        i++;
        if (c == 0x30)
        {
          whatlang = JAPANESE;
        }
      }
    }
    return whatlang;
  } // public static int detect_jpn(String text)
} // public class JapaneseDetector